<?php
/**
 * automatically creates the necessary 
 * runtime/ and assets/ folders
 * under the required folders
 *
 * @imehesz 
 */
class SetupRuntimeAssetsCommand extends CConsoleCommand
{
	public function getHelp()
	{
		return <<<EOD
USAGE
  setupruntimeassets

DESCRIPTION
  Automatically (tries) to create the necessary `runtime` and `assets` folders 
  under the required Yii folders. 
  
  If fails, please create the following folders manually:
  - console/runtime
  - frontend/runtime
  - frontend/www/assets

  (Also make sure that these folders are writable by the webserver.)

EOD;
	}

	public function run( $args )
	{
		chdir(dirname(__FILE__).'/../../');

		$console_path = 'console/';
		$frontend_path = 'frontend/';

		$messages = array();

		$ok = true;

		if( ! file_exists( $console_path . 'runtime' ) )
		{
			// create console runtime
			if( ! mkdir( $console_path . 'runtime'  ) )
			{
				echo "Failed to create console `runtime` path! \r\n";
				$ok = false;
			}
		}

		if( ! file_exists( $frontend_path . 'runtime' ) )
		{
			// create frontend runtime path
			if( ! mkdir( $frontend_path . 'runtime' ) )
			{
				echo "Failed to create frontend `runtime` path! \r\n";
				$ok = false;
			}
		}

		if( ! file_exists( $frontend_path . 'www/assets' ) )
		{
			// create assets path
			if( ! mkdir( $frontend_path . 'www/assets' ) )
			{
				echo "Failed to create frontend `www/assets` path! \r\n";
				$ok = false;
			}
		}

		// if everything is OK we set the permissions
		if( $ok )
		{
			chmod( $console_path . 'runtime', 0777 );
			chmod( $frontend_path . 'runtime', 0777 );
			chmod( $frontend_path . 'www/assets', 0777 );
		}

		echo $ok ? "OK!\r\n" : "FAIL!\r\n";
	}
}
